package com.lianjing.sh.acceleo.controller.mvc.controller;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

import org.apache.commons.lang3.StringUtils;
import org.eclipse.uml2.uml.NamedElement;
import org.eclipse.uml2.uml.UseCase;

import com.lianjing.sh.acceleo.controller.tool.service.NameService;

public class ActionService {
	
	private static final NameService NameService = new NameService();
	
	public String getActionSimpleName(UseCase useCase){
		String name = NameService.getSimpleName(useCase);
		if(null!=name && !name.trim().equals("")){
			return name.substring(0,1).toUpperCase() + name.substring(1);
		}
		// 默认名称
		if(isPage(useCase)){
			return "Page";
		}else if(isAccessPage(useCase)){
			return "PageAccess";
		}else if(isList(useCase)){
			return "List";
		}else if(isAccessList(useCase)){
			return "ListAccess";
		}else if(isAdd(useCase)){
			return "Add";
		}else if(isAccessAdd(useCase)){
			return "AddAccess";
		}else if(isEdit(useCase)){
			return "Edit";
		}else if(isAccessEdit(useCase)){
			return "EditAccess";
		}else if(isTree(useCase)){
			return "Tree";
		}else if(isAccessTree(useCase)){
			return "TreeAccess";
			
		}else if(isAccessDetail(useCase)){
			return "DetailAccess";
		}else if(isAccessCommon(useCase)){
			return "Access";
			
		}else if(isExcelExport(useCase)){
			return "ExcelExport";
		}else if(isExcelImport(useCase)){
			return "ExcelImport";
			
		}else if(isFileUpload(useCase)){
			return "FileUpload";
		}else if(isFileDownload(useCase)){
			return "FileDownload";
		}else if(isShowImg(useCase)){
			return "ShowImg";
			
		}else if(isSubmit(useCase)){
			return "Submit";
		}else if(isSubmits(useCase)){
			return "Submits";
			
		}else if(isGet(useCase)){
			return "Get";
		}else if(isPost(useCase)){
			return "Post";
		}else if(isPut(useCase)){
			return "Put";
		}else if(isDelete(useCase)){
			return "Delete";
		}else if(isDeletes(useCase)){
			return "Deletes";
			
		}else if(isMessage(useCase)){
			return "Message";
		}else{
			List<String> allOwningPackages = useCase.allOwningPackages().stream().map(it->NameService.getSimpleName(it)).collect(Collectors.toList());
			Collections.reverse(allOwningPackages);
			throw new RuntimeException(StringUtils.join(allOwningPackages, ".") + "." + ((NamedElement)useCase.getOwner()).getName() + "." + useCase + ": 缺少Action类型的Profile");
		}
//		isAccessIndex(useCase) 
	}
	
	public String getActionComment(UseCase useCase){
		String comment = NameService.getNameHead(useCase);
		if(null!=comment && !comment.trim().equals("")){
			return comment;
		}
		// 默认名称
		if(isPage(useCase)){
			return "分页列表";
		}else if(isAccessPage(useCase)){
			return "访问分页列表页面";
		}else if(isList(useCase)){
			return "列表";
		}else if(isAccessList(useCase)){
			return "访问列表页面";
		}else if(isAdd(useCase)){
			return "添加";
		}else if(isAccessAdd(useCase)){
			return "访问添加页面";
		}else if(isEdit(useCase)){
			return "编辑";
		}else if(isAccessEdit(useCase)){
			return "访问编辑页面";
		}else if(isTree(useCase)){
			return "分类树";
		}else if(isAccessTree(useCase)){
			return "访问分类树页面";
			
		}else if(isAccessDetail(useCase)){
			return "访问详情页面";
		}else if(isAccessCommon(useCase)){
			return "访问页面";
			
		}else if(isExcelExport(useCase)){
			return "导出Excel文件";
		}else if(isExcelImport(useCase)){
			return "导入Excel数据";
			
		}else if(isFileUpload(useCase)){
			return "上传文件";
		}else if(isFileDownload(useCase)){
			return "下载文件";
		}else if(isShowImg(useCase)){
			return "显示图片";
			
		}else if(isSubmit(useCase)){
			return "提交";
		}else if(isSubmits(useCase)){
			return "提交";
			
		}else if(isGet(useCase)){
			return "Get请求";
		}else if(isPost(useCase)){
			return "Post请求";
		}else if(isPut(useCase)){
			return "Put请求";
		}else if(isDelete(useCase)){
			return "删除";
		}else if(isDeletes(useCase)){
			return "删除";
			
		}else if(isMessage(useCase)){
			return "Message请求";
		}else{
			throw new RuntimeException(useCase + ": 缺少Action类型的Profile");
		}
//		isAccessIndex(useCase) 
	}
	
	public Boolean isAction(UseCase useCase){
		return isAccess(useCase) || isNotAccess(useCase);
	}
	
	/**
	 * Access
	 */
	public Boolean isAccessWithData(UseCase useCase){
		return isActionType(useCase, "AccessWithData");
	}
	
	public Boolean isAccess(UseCase useCase){
		return 
			isAccessIndex(useCase) 
			|| isAccessDetail(useCase)
			|| isAccessList(useCase)
			|| isAccessPage(useCase)
			|| isAccessAdd(useCase)
			|| isAccessEdit(useCase)
			|| isAccessTree(useCase)
			|| isAccessCommon(useCase);
	}
	public Boolean isAccessCommon(UseCase useCase){
		return isActionType(useCase, "CommonAccess");
	}
	public Boolean isAccessIndex(UseCase useCase){
		return isActionType(useCase, "IndexAccess");
	}
	public Boolean isAccessPage(UseCase useCase){
		return isActionType(useCase, "PageAccess");
	}
	public Boolean isAccessList(UseCase useCase){
		return isActionType(useCase, "ListAccess");
	}
	public Boolean isAccessDetail(UseCase useCase){
		return isActionType(useCase, "DetailAccess");
	}
	public Boolean isAccessAdd(UseCase useCase){
		return isActionType(useCase, "AddAccess");
	}
	public Boolean isAccessEdit(UseCase useCase){
		return isActionType(useCase, "EditAccess");
	}
	public Boolean isAccessTree(UseCase useCase){
		return isActionType(useCase, "TreeAccess");
	}
	
	/**
	 * isNotAccess
	 */
	public Boolean isNotAccess(UseCase useCase){
		return 
			isPage(useCase) 
			|| isList(useCase)
			|| isTree(useCase)
			|| isAdd(useCase)
			|| isEdit(useCase)
			
			|| isExcelExport(useCase)
			|| isExcelImport(useCase)
			|| isFileUpload(useCase)
			|| isFileDownload(useCase)
			|| isShowImg(useCase)
			
			|| isSubmit(useCase)
			|| isSubmits(useCase)
			
			|| isGet(useCase)
			|| isPost(useCase)
			|| isPut(useCase)
			|| isDelete(useCase)
			|| isDeletes(useCase)
			
			|| isMessage(useCase);
	}
	public Boolean isPage(UseCase useCase){
		return isActionType(useCase, "Page");
	}
	public Boolean isList(UseCase useCase){
		return isActionType(useCase, "List");
	}
	public Boolean isTree(UseCase useCase){
		return isActionType(useCase, "Tree");
	}
	public Boolean isAdd(UseCase useCase){
		return isActionType(useCase, "Add");
	}
	public Boolean isEdit(UseCase useCase){
		return isActionType(useCase, "Edit");
	}
	
	public Boolean isExcelExport(UseCase useCase){
		return isActionType(useCase, "ExcelExport");
	}
	public Boolean isExcelImport(UseCase useCase){
		return isActionType(useCase, "ExcelImport");
	}
	public Boolean isFileUpload(UseCase useCase){
		return isActionType(useCase, "FileUpload");
	}
	public Boolean isFileDownload(UseCase useCase){
		return isActionType(useCase, "FileDownload");
	}
	public Boolean isShowImg(UseCase useCase){
		return isActionType(useCase, "ShowImg");
	}
	
	public Boolean isSubmit(UseCase useCase){
		return isActionType(useCase, "Submit");
	}
	public Boolean isSubmits(UseCase useCase){
		return isActionType(useCase, "Submits");
	}
	
	public Boolean isGet(UseCase useCase){
		return isActionType(useCase, "Get");
	}
	public Boolean isPost(UseCase useCase){
		return isActionType(useCase, "Post");
	}
	public Boolean isPut(UseCase useCase){
		return isActionType(useCase, "Put");
	}
	public Boolean isDelete(UseCase useCase){
		return isActionType(useCase, "Delete");
	}
	public Boolean isDeletes(UseCase useCase){
		return isActionType(useCase, "Deletes");
	}
	
	public Boolean isMessage(UseCase useCase){
		return isActionType(useCase, "Message");
	}
	
	private Boolean isActionType(UseCase useCase, String type){
		if(null == useCase.getAppliedStereotype("profile::web::action::Action_"+type)){
			return false;
		}else{
			return true;
		}
	}
	
}
